package com.gaohui.nestedrecyclerview.java;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.gaohui.nestedrecyclerview.R;
import com.gaohui.nestedrecyclerview.java.adapter.MultiTypeAdapter;
import com.gaohui.nestedrecyclerview.java.bean.CategoryBean;
import com.gaohui.nestedrecyclerview.java.view.ParentRecyclerView;
import com.gaohui.nestedrecyclerview.java.view.StoreSwipeRefreshLayout;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @author : xfhy
 * Create time : 2021/3/8 7:11 AM
 * Description :
 */
public class WeatherFragment extends Fragment {

    ArrayList<Object> mDataList = new ArrayList<Object>();

    MultiTypeAdapter adapter = new MultiTypeAdapter(mDataList);

    ParentRecyclerView javaRecyclerView;

    String[] strArray = new String[]{"推荐", "视频", "直播", "图片", "精华", "热门"};
    private RefreshLayout refreshLayout;

    public static WeatherFragment newInstance() {

        Bundle args = new Bundle();

        WeatherFragment fragment = new WeatherFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_weather, container, false);
        initView(view);
        return view;
    }

    private void initView(final View view) {
        javaRecyclerView = view.findViewById(R.id.javaRecyclerView);

        javaRecyclerView.setAdapter(adapter);

        javaRecyclerView.initLayoutManager(getContext());
        javaRecyclerView.setParentRvListener(new ParentRecyclerView.ParentRvListener() {
            @Override
            public void onScrollEnd() {
                //todo xfhy 待实现  展示标题栏
                //view.findViewById(R.id.cl_title).setVisibility(View.VISIBLE);

                //外层的刷新器就别在刷新了
                refreshLayout.setEnableRefresh(false);
            }
        });

        refreshLayout = view.findViewById(R.id.swipeRefreshLayout);
        refreshLayout.setRefreshHeader(new ClassicsHeader(getContext()));
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NotNull RefreshLayout refreshlayout) {
                //refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                refresh();
            }
        });

        refresh();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    private void refresh() {
        mDataList.clear();
        mDataList.add(111);
        for (int i = 0; i < 30; i++) {
            mDataList.add("parent item text " + i);
        }
        CategoryBean categoryBean = new CategoryBean();
        categoryBean.getTabTitleList().clear();
        categoryBean.getTabTitleList().addAll(Arrays.asList(strArray));
        mDataList.add(categoryBean);
        adapter.notifyDataSetChanged();
        refreshLayout.finishRefresh();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        adapter.destroy();
    }
}
